/*This file is part of PaperBall.

PaperBall is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

PaperBall is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with PaperBall.  If not, see <http://www.gnu.org/licenses/>.*/

package com.ran3000.paperball;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import com.ran3000.paperball.framework.Game;
import com.ran3000.paperball.framework.Input.TouchEvent;
import com.ran3000.paperball.framework.gl.Camera2D;
import com.ran3000.paperball.framework.gl.SpriteBatcher;
import com.ran3000.paperball.framework.math.OverlapTester;
import com.ran3000.paperball.framework.math.Rectangle;
import com.ran3000.paperball.framework.math.Vector2;

public class HighScoreScreen extends GLScreen {
	Camera2D guiCam;
	SpriteBatcher batcher;
	Rectangle exitBounds;
	Vector2 touchPoint;
	String[] highScores;
	float xOffset = 0;
	
	public HighScoreScreen(Game game) {
		super(game);
		
		guiCam = new Camera2D(glGraphics, 480, 320);
		exitBounds = new Rectangle(422, 10, 48, 48);
		touchPoint = new Vector2();
		batcher = new SpriteBatcher(glGraphics, 100);
		highScores = new String[5];
		for(int i = 0; i < 5; i++) {
			highScores[i] = (i + 1) + ". " + Settings.highscores[i];
			xOffset = Math.max(highScores[i].length() * Assets.font.glyphWidth, xOffset);
		}
		xOffset = 265 - xOffset / 2;
	}
	
	@Override
	public void update(float deltaTime) {
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		game.getInput().getKeyEvents();
		int len = touchEvents.size();
		for(int i = 0; i < len; i++){
			TouchEvent event = touchEvents.get(i);
			touchPoint.set(event.x, event.y);
			guiCam.touchToWorld(touchPoint);
			
			if(event.type == TouchEvent.TOUCH_UP) {
				if(OverlapTester.pointInRectangle(exitBounds, touchPoint)) {
					Assets.playSound(Assets.clickSound);
					game.setScreen(new MainMenuScreen(game));
					return;
				}
			}
		}
	}
	
	@Override
	public void present(float deltaTime) {
		GL10 gl = glGraphics.getGL();
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		guiCam.setViewportAndMatrices();
		
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		

		batcher.beginBatch(Assets.items);
		batcher.drawSprite(240, 160, 480, 320, Assets.background2);
		batcher.drawSprite(240, 160, 480, 320, Assets.computer);
		batcher.drawSprite(446, 34, 48, 48, Assets.exitCircle);
		float y = 170;
		for(int i = 4; i >= 0; i--) {
			Assets.font.drawText(batcher, highScores[i], xOffset, y);
			y += Assets.font.glyphHeight;
		}
		batcher.endBatch();
		
		gl.glDisable(GL10.GL_BLEND);
	}
	
	@Override
	public void resume() {
	}
	
	@Override
	public void pause() {
	}
	
	@Override
	public void dispose() {
	}

}
